$(function(){

    var vipDatagrid, vip_edit_remove, vipDialog, vipForm, vipSearch;
    vipDatagrid = $("#vip_datagrid");
    vip_edit_remove = $("#vip_edit,#vip_remove");
    vipDialog = $("#vip_dialog");
    vipForm = $("#vip_form");
    vipSearch = $("#vip_search");

    vipDatagrid.datagrid({
        fit:true,
        url:"/vip/list",
        fitColumns:true,
        pagination:true,
        rownumbers:true,
        singleSelect:true,
        toolbar:"#vip_tb",
        columns:[
            [
                /*{field:"id",title:"会员编号",width:100,align:"center"},*/
                {field:"card",title:"会员卡",width:100,align:"center"},
                {field:"realname",title:"会员姓名",width:100,align:"center"},
                {field:"tag",title:"标签",width:100,align:"center"},
                {field:"tel",title:"电话",width:100,align:"center"},
                {field:"group",title:"分组",width:100,align:"center"},
                {field:"grade",title:"会员等级",width:100,align:"center"},
                {field:"usableScore",title:"可用积分",width:100,align:"center"},
                {field:"email",title:"邮箱",width:100,align:"center"},
                {field:"address",title:"客户地址",width:100,align:"center"},
                {field:"balance",title:"储值余额",width:100,align:"center"}
            ]
        ],
        onClickRow:function(index,row){
            if(row.state==1){
                //已离职,不允许编辑,离职操作
                vip_edit_remove.linkbutton("disable");
            }else{
                vip_edit_remove.linkbutton("enable");
            }
        }
    });
    //对话框
    vipDialog.dialog({
        width:450,
        height:450,
        buttons:"#vip_btn",
        closed:true,
        modal:true,
        constrain:true  //限制窗体的位置,只能在父窗体的范围内活动
    });
    //高级查询搜索框
    vipSearch.textbox({
        buttonIcon:"icon-search",
        label:"关键字 :",
        buttonText:"搜索",
        labelWidth:"50px",
        prompt:"会员姓名 / 会员卡号",
        onClickButton:function(){
            //利用datagrid发送远程加载
            var keyword = vipSearch.val();
            vipDatagrid.datagrid("load",{
                //会自动提交keyword这个参数,发送/vip/list请求,自动渲染表格数据
                keyword:keyword
            });
        }
    });


    //编写方法命令对象
    var cmdObj = {
        add:function(){
            vipDialog.dialog("open");
            vipDialog.dialog("setTitle","新增");
            vipForm.form("clear");
        },

        edit:function(){
            var selectRow = vipDatagrid.datagrid("getSelected");
            if(selectRow){
                vipDialog.dialog("open");
                vipDialog.dialog("setTitle","编辑");
                vipForm.form("clear");
                vipForm.form("load",selectRow);
            }else{
                $.messager.alert("温馨提示!","请选择一条要编辑的数据","info");
            }
        },

        //保存方法,和更新方法共同拥有
        save:function(){
            vipForm.form("submit",{
                url:"/vip/saveOrUpdate",

                //提交额外的参数到后台
                onSubmit: function (param) {
                    var initBalance = $("[name=initBalance]").val();
                    var giveBalance = $("[name=giveBalance]").val();
                    var balance = parseFloat(initBalance)+parseFloat(giveBalance);
                    console.log(balance);
                    param.balance = balance;
                    return true;
                },

                success:function(data){
                    data = $.parseJSON(data);
                    if(data.success){
                        //成功,则刷新表格,并且提示保存成功
                        vipDatagrid.datagrid("reload");
                        $.messager.alert("温馨提示!",data.msg,"info");
                        vipDialog.dialog("close");
                    }else{
                        $.messager.alert("温馨提示!",data.msg,"error");
                    }
                }
            });
        },

        remove:function(){
            var selectRow = vipDatagrid.datagrid("getSelected");
            if(selectRow){
                $.messager.confirm("温馨提示","确定要离职该会员吗?", function (yes) {
                    if (yes){
                        $.get("/vip/quit?id="+selectRow.id, function (data) {
                            if(data.success){
                                vipDatagrid.datagrid("reload");
                                $.messager.alert("温馨提示!",data.msg,"info");
                            }else{
                                $.messager.alert("温馨提示!",data.msg,"error");
                            }
                        },"json")
                    }
                });
            }else{
                $.messager.alert("温馨提示!","请选择要离职的会员","info");
            }
        },

        reload:function(){
            vipDatagrid.datagrid("reload");
        },

        cancel:function(){
            vipDialog.dialog("close");
        }
    }

    //==========调用方法
    $("a[data-cmd]").on("click", function () {
        var cmd = $(this).data("cmd");
        cmdObj[cmd]();
    })
});

function deptFormatter(value,row,index){
    return value?value.name:"";
}

function stateFormatter(value,row,index){
    return value==0?"<font color='green'>在职</font>":"<font color='red'>离职</font>";
}

function adminFormatter(value,row,index){
    return value?"是":"否";
}